<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }
    public function _initialize()
    {
    	if(!session('?admin')){
    		$this->error('没有权限','../Login/login');
    		exit;
    	}
    }
 
	 public function users(){
		$User = M('Users'); // 实例化User对象
		$count = $User->count();// 查询满足要求的总记录数
		$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(5)
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

		

		$list = $User->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

		/*$this->assign('users',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出*/
		
		$this->users=$list;
		$this->page=$show;
		
		$this->display(); // 输出模板
	}
	public function del($id){
		$usersmodel=M('Users');
		if ($usersmodel->delete($id)) {
			$this->success('删除成功');

		}else{
			$this->error('删除失败');
		}
	}
	public function setstar($id){
		$usersmodel=M('Users');
		$usersmodel->id= $id;
		$usersmodel->isstar = 1;
		if ($usersmodel->save()) {
		 	$this->success('修改成功');
		 } else{
			$this->error('修改失败');
		}

	}
	public function unstar($id){
		$usersmodel=M('Users');
		$usersmodel->id= $id;
		$usersmodel->isstar = 0;
		$usersmodel->isfirst = 0;
		if ($usersmodel->save()) {
		 	$this->success('修改成功');
		 } else{
			$this->error('修改失败');
		}

	}
	public function getstate0($id){
		$usersmodel=M('Users');
			$usersmodel->id= $id;
			$usersmodel->state = 0;
			if ($usersmodel->save()) {
			 	$this->success('修改成功');
			 } else{
				$this->error('修改失败');
			}
		}
		
	
	public function getstate1($id){
		$usersmodel=M('Users');
			$usersmodel->id= $id;
			$usersmodel->state = 1;
			if ($usersmodel->save()) {
			 	$this->success('修改成功');
			 } else{
				$this->error('修改失败');
			}
		}
		
	
	public function getstate2($id){
		$usersmodel=M('Users');
			$usersmodel->id= $id;
			$usersmodel->state = 2;
			if ($usersmodel->save()) {
			 	$this->success('修改成功');
			 } else{
				$this->error('修改失败');
			}
		}
		
	
	public function setfirst($id,$isstar){
		if($isstar==0){
			$this->error('ta不是明星');
		}else{
			$usersmodel=M('Users');
			$usersmodel->id= $id;
			$usersmodel->isfirst = 1;
			if ($usersmodel->save()) {
			 	$this->success('修改成功');
			 } else{
				$this->error('修改失败');
			}
		}
	}
	public function unfirst($id){
		$usersmodel=M('Users');
		$usersmodel->id= $id;
		$usersmodel->isfirst = 0;
		if ($usersmodel->save()) {
		 	$this->success('修改成功');
		 } else{
			$this->error('修改失败');
		}

	}

	public function modify($id){
		$usersmodel=M('Users');
		$aa=$usersmodel->where("id=$id")->select();
		$this->aa=$aa;

		$this->display();

	}
		public function userupdate(){
		/*$usersmodel=M('Users');
		$usersmodel->id=$_POST['id'];
		$usersmodel->username=$_POST['username'];
		$usersmodel->realname=$_POST['realname'];
		$usersmodel->email=$_POST['email'];
		$usersmodel->tele=$_POST['tele'];
		$usersmodel->state=$_POST['state'];
		$usersmodel->intro=$_POST['intro'];
		$usersmodel->isstar=$_POST['isstar'];
		$usersmodel->isfirst=$_POST['isfirst'];*/

		$usersmodel=M('Users');
		$usersmodel->create();

		if($_FILE['photo']['error']==0){
			$photoinfo=$this->upload();
			$usersmodel->photo=$photoinfo['photo']['savename'];
		}

		if ($usersmodel->save()) {
				 $this->success('修改成功','users',4);
		} else{
				
				$this->error("修改失败");
		}

	}
		
	public function upload(){
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Public/uploads/photo/'; // 设置附件上传根目录
	   
	   	$upload->autoSub=false;
	    $info= $upload->upload();  // 上传文件 
	    if(!$info) {
	           $this->error($upload->getError()); 
	    }
		else{ 
		      return $info;    
		  }                    

	}

	public function useradd(){
		

		$user=D("Users");
		if($user->create()){
		
		if($_FILE['photo']['error']==0){
			$photoinfo=$this->upload();
			$user->photo=$photoinfo['photo']['savename'];
		}else{
			$user->photo='defolse.jpg';
		}
		$user->verifyinfo="待审核";
		
		if ($user->add()) {
				 $this->success('添加成功','users',4);
		} else{
				
				$this->error("添加失败");
		}
	}else{
          $this->error($user->getError());  
     }  
}
	


	

}